<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    #box{
        width: 100px;
        height: 100px;;
        background: rgb(131, 88, 95);
        position: absolute;
        left: 0px;
        top: 0;
    }
    input{
        position: absolute;
        z-index: 10;
        left: 0;
        top:0;
    }
    </style>
</head>
<body>
    <input type="button" value="按钮" id="btn">
    <div id="box"></div> 
    <script>
        //获取任意一个元素的任意一个属性的当前的值---当前属性的位置值
        function getStyle(element,attr){
            if(window.getComputedStyle){
                return window.getComputedStyle(element,null)[attr];
            }else{
                return element.currentStyle[attr];
            }
        }
        //getStyle(box,'top')
        var box = document.getElementById('box'); 

        function animation(element,json,fn){
            clearInterval(element.timer);
            element.timer=setInterval(function(){
                var flag = true;//假设全部到达目标
                for(var attr in json){
                    if(attr == "opacity"){
                        var current = getStyle(element,attr)*100;
                        var target = json[attr]*100;
                        var step = (target-current)/10;
                        step=step>0?Math.ceil(step):Math.floor(step);
                        current+=step;
                        element.style[attr] = current/100;
                    }else if(attr == "zIndex"){
                        element.style[attr] = json[attr];
                    }else{
                        var current = parseInt(getStyle(element,attr));
                        var target = json[attr];
                        var step = (target-current)/10;
                        step=step>0?Math.ceil(step):Math.floor(step);
                        current+=step;
                        element.style[attr] = current+'px';
                    }
                    if(current != target){
                        flag=false;
                    }
                }

                if (flag) {
                    //清理定时器
                    clearInterval(element.timer);
                    //所有的属性到达目标才能使用这个函数,前提是用户传入了这个函数
                    if (fn) {
                    fn();
                    }
                }

            },20)
        }
        
        document.getElementById('btn').onclick=function(){
            var json={"width": 400, "height": 500, "left": 500, "top": 80, "opacity": 0.2};
            animation(box,json,function(){
                animation(box,{"width": 10, "height": 50, "left": 0, "top": 0, "opacity": 1, "zIndex": 1000});
            })
        }
    </script>
</body>
</html>